package edu.isi.pedwork;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class InitDB {
	public static String DB_CONN = Configure.dbPath;
	private Connection conn = null;
	private Statement stmt = null;

	private InitDB() {
		try {
			init_db(DB_CONN);
			stmt.execute("delete from topic_queue");
			ResultSet rs = stmt.executeQuery("Select UserId from users");
			while (rs.next()) {
				PreparedStatement INSERT = conn
						.prepareStatement("insert into topic_queue (userId,date) values (?,?) ");
				INSERT.setString(1, rs.getString("UserId"));
				INSERT.setString(2, "" + (new Date()).getTime() / 1000);
				INSERT.execute();
			}

			close_db();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public Connection getConnection(String url) throws Exception {
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		return DriverManager.getConnection(url);
	}

	public void init_db(String connect) throws SQLException {
		conn = DriverManager.getConnection(connect);
		stmt = conn.createStatement();
	}

	public void close_db() throws SQLException {
		stmt.close();
		conn.close();
	}

	public static void main(String args[]) {
		InitDB db = new InitDB();
	}
}
